package com.lanou.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;

/**
 * @author zyj
 * @date 2022年06月01日 17:12
 */
public class Test {
    public static void main(String[] args) {
        //获取Class的对象
        //通过类名.class
        Class c = A.class;
        //通过对象调用getClass()
        Class c1 = new A().getClass();
        //类加载器获取
        try {
            A.class.getClassLoader().loadClass("com.lanou.reflect.A");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //通过Class.forName()
        try {
            Class clazz = Class.forName("com.lanou.reflect.A");
            //获取类上面的注解
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation a : annotations) {
                System.out.println("注解名称:" + a.annotationType().getName());
            }
            //获取父类（直接父类）
            Class superClazz = clazz.getSuperclass();
            System.out.println("父类：" + superClazz);
            //获取父接口（直接父接口）
            Class[] inter = clazz.getInterfaces();
            for (Class cl : inter) {
                System.out.println(cl);
            }

            //获取公共属性
            //getField(String name)通过属性名去获取
            //getFields()获取所有的公共属性
            Field[] fields = clazz.getFields();
            for (Field f:fields) {
                System.out.println("属性名:" + f.getName() + " 属性类型：" + f.getType());
            }

            //获取所有属性
            System.out.println("*****************************");
            Field[] fields1 = clazz.getDeclaredFields();
            for (Field f:fields1) {
                System.out.println("属性名:" + f.getName() + " 属性类型：" + f.getType()
                        + "权限：" + f.getModifiers());
                System.out.println(Modifier.isPublic(f.getModifiers()));
            }

            //获取所有有的构造方法

            Constructor[] constructors = clazz.getDeclaredConstructors();
            for (Constructor con:constructors) {
                System.out.print(con.getName() + "(");
                Class[] parameterType = con.getParameterTypes();
                for (Class cl:parameterType) {
                    System.out.print(cl + ",");
                }
                System.out.println(")");
            }

            //通过Constructor去创建对象
            Constructor constructor = clazz.getConstructor(String.class,int.class);

            A a = (A)constructor.newInstance("",1);
           A a1 =  (A)clazz.newInstance();

           Method[] methods = clazz.getDeclaredMethods();
            for (Method m:methods) {
                System.out.print(m.getName() + "(");
               Class[] cl = m.getParameterTypes();
                for (Class cla:cl) {
                    System.out.print(cla.getName() + ",");
                }
                System.out.println(")");
            }

           Method m =  clazz.getDeclaredMethod("add1",String.class);
            System.out.println(m.invoke(a,"占山"));
            Method m1 =  clazz.getDeclaredMethod("add2",String.class);
            m1.setAccessible(true);
            m1.invoke(a,"占山");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
